package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.EvaluateImageResponse
import com.gexiaobao.pigeon.app.model.bean.MerchantStoresInfoResponse
import com.gexiaobao.pigeon.app.model.bean.StoreInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.CreateShopParam
import com.gexiaobao.pigeon.app.model.param.EditShopInfoParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2023/6/27 15:20
 * @Describe    : 店铺信息
 */
class MerchantStoresViewModel : BaseViewModel() {

    private var pageSize = 20
    private var pageNo: Int = 1

    /** 进店首页 */
    val storeGoodListResult: MutableLiveData<ListDataUiState<MerchantStoresInfoResponse.ListData>> = MutableLiveData()
    fun getAppShopIndex(isRefresh: Boolean, shopId: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getAppShopIndex(pageNo, pageSize, shopId) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                shopName = it.shopName,
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            storeGoodListResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<MerchantStoresInfoResponse.ListData>()
                )
            storeGoodListResult.value = listDataUiState
        })
    }

    /** 进店首页 */
    val storeInfoResult: MutableLiveData<MerchantStoresInfoResponse> = MutableLiveData()
    fun getAppShopIndex(shopId: Int) {
        request({ apiService.getAppShopIndexInfo(pageNo, pageSize, shopId) }, {
            storeInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取店铺信息 */
    val mineStoreInfoResult: MutableLiveData<StoreInfoResponse> = MutableLiveData()
    fun getAppShopInfo() {
        request({ apiService.getAppShopInfo() }, {
            mineStoreInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 创建店铺 */
    val createStoreResult: MutableLiveData<UiState> = MutableLiveData()
    fun createShop(body: CreateShopParam) {
        request({ apiService.createShop(body) }, {
            val result = UiState(isSuccess = true)
            createStoreResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            createStoreResult.value = result
        })
    }

    /** 编辑店铺 */
    val editStoreResult: MutableLiveData<UiState> = MutableLiveData()
    fun editAppShop(body: EditShopInfoParam) {
        request({ apiService.editAppShop(body) }, {
            val result = UiState(isSuccess = true)
            editStoreResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            editStoreResult.value = result
        })
    }

    /** 上传文件 */
    val evaluateImageResult: MutableLiveData<EvaluateImageResponse> = MutableLiveData()
    fun uploadFile(imageFile: File) {
        val body: RequestBody = imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull()) //表单类型
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)//表单类型
        builder.addFormDataPart("upLoadFile", imageFile.name, body) //添加图片数据，body创建的请求体
        val requestBody = builder.build()

        request({ apiService.uploadFile(requestBody) }, {
            evaluateImageResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }
}